//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.StandardAddIn.Statistics
{
    internal abstract class Solver
    {
        private double absoluteAccuracy = 1E-15;

        internal DoubleFunction f;
        private double functionValueAccuracy = 1E-15;
        private int _maximalIterationCount;

        protected Solver(
            DoubleFunction f,
            int defaultMaximalIterationCount,
            double defaultAbsoluteAccuracy)
        {
            Contract.Requires(f != null);
            this.f = f;
            absoluteAccuracy = defaultAbsoluteAccuracy;
            MaximalIterationCount = defaultMaximalIterationCount;
        }


        public double AbsoluteAccuracy
        {
            get
            {
                Contract.Ensures(Contract.Result<double>() >= 0);
                return absoluteAccuracy;
            }
            set
            {
                Contract.Requires(value >= 0);
                absoluteAccuracy = value;
            }
        }

        public double FunctionValueAccuracy
        {
            get
            {
                Contract.Ensures(Contract.Result<double>() >= 0);
                return functionValueAccuracy;
            }
            set
            {
                Contract.Requires(value >= 0);
                functionValueAccuracy = value;
            }
        }

        public int MaximalIterationCount
        {
            get
            {
                Contract.Ensures(Contract.Result<int>() > 0);
                return _maximalIterationCount;
            }
            set
            {
                Contract.Requires(value > 0);
                _maximalIterationCount = value;
            }
        }


        public abstract double Solve(double min, double max);
    }
}